
Type CalCoreModel

	Field m_name:String
	Field m_coreSkeleton:CalCoreSkeleton
	Field m_vectorCoreAnimation:CalCoreAnimation[]
	Field m_vectorCoreMorphAnimation:CalCoreMorphAnimation[]
	Field m_vectorCoreAnimatedMorph:CalCoreAnimatedMorph[]
	Field m_vectorCoreMesh:CalCoreMesh[]
	Field m_vectorCoreMaterial:CalCoreMaterial[]
	Field m_mapmapCoreMaterialThread:IntMap
	Field m_animationName:StringMap
	Field m_materialName:StringMap
	Field m_meshName:StringMap
	Field m_userData:Object
	
	
	Method New(name:String="Model")

		m_name = name	
		m_coreSkeleton = Null
		m_vectorCoreAnimation = New CalCoreAnimation[0]
		m_vectorCoreMorphAnimation = New CalCoreMorphAnimation[0]
		m_vectorCoreAnimatedMorph= New CalCoreAnimatedMorph[0]
		m_vectorCoreMesh = New CalCoreMesh[0]
		m_vectorCoreMaterial = New CalCoreMaterial[0]
		m_mapmapCoreMaterialThread = New IntMap
		m_animationName = New StringMap
		m_materialName = New StringMap
		m_meshName = New StringMap
		
		
	End Method
	
	Method getUserData:Object()
		Return m_userData
	End Method
	
	Method setUserData(userData:Object)
		m_userData = userData
	End Method
	
	Method scale(factor:Float)
		m_coreSkeleton.scale(factor)
	
		For Local animationId:Int = 0 To m_vectorCoreAnimation.Length-1
			m_vectorCoreAnimation[animationId].scale(factor)
		Next
	
		For Local meshId:Int = 0 To m_vectorCoreMesh.Length-1
			m_vectorCoreMesh[meshId].scale(factor)
		Next	
	
	End Method
	
	Method addCoreAnimation:Int(coreAnimation:CalCoreAnimation)
		
		Local animationId = m_vectorCoreAnimation.length
		m_vectorCoreAnimation = m_vectorCoreAnimation[..animationId+1] 'push(coreAnimation)
		m_vectorCoreAnimation[animationId] = coreAnimation
		Return animationId		
	
	End Method
	
	Method getCoreAnimation:CalCoreAnimation(coreAnimationId:Int)

		If coreAnimationId < 0 Or coreAnimationId >= m_vectorCoreAnimation.length
			DebugLog "Invalid Animation ID"
			Return Null
		EndIf
	
		Return m_vectorCoreAnimation[coreAnimationId]		
	
	End Method

	Method getCoreAnimationCount:Int()
		Return m_vectorCoreAnimation.length
	End Method
	
	Method loadCoreAnimation:Int(filename:String)
		DebugLog "Not Implemented! use Loader Type"
		Return -1
	End Method
	
	Method unloadCoreAnimationbyID:Int(coreAnimationId:Int)
	
		If coreAnimationId < 0 Or coreAnimationId >= m_vectorCoreAnimation.length
			DebugLog "Invalid Animation ID"
			Return -1;
		EndIf
	
		m_vectorCoreAnimation[coreAnimationId] = Null
		Return coreAnimationId
	
	End Method

	Method unloadCoreAnimation:Int(coreAnimationId:String)
		Local aid:Int = getCoreAnimationId(coreAnimationId)
		If aid < 0 Or aid >= m_vectorCoreAnimation.length
			DebugLog "Invalid Animation ID"
			Return -1;
		EndIf
	
		m_vectorCoreAnimation[aid] = Null
		Return aid
	
	End Method


	Method saveCoreAnimation:Byte(filename:String, coreAnimationId:Int)
		DebugLog "Not Implemented! use Saver Type"
	End Method
	

	Method addAnimationName:Byte(name:String, coreAnimationId:Int)
	
		If coreAnimationId < 0 Or coreAnimationId >= m_vectorCoreAnimation.length
			DebugLog "Invalid Animation ID"
			Return False
		EndIf
	
		m_vectorCoreAnimation[coreAnimationId].setName(name)
		m_animationName.Set(name,coreAnimationId)
		Return True
		
	End Method
	
	Method getCoreAnimationId:Int(animationName:String)
	
		If m_animationName.Contains(animationName) = True Then
			Return Int(m_animationName.Get(animationName))
		Else
			Return -1
		EndIf
	
	End Method
	
	Method addCoreMorphAnimation:Int(coreMorphAnimation:CalCoreMorphAnimation)
	
		Local morphAnimationId:Int = m_vectorCoreMorphAnimation.length
		m_vectorCoreMorphAnimation = m_vectorCoreMorphAnimation[..morphAnimationId+1]'push(coreMorphAnimation)
		m_vectorCoreMorphAnimation[morphAnimationId] = coreMorphAnimation
		Return morphAnimationId		
	
	End Method
	
	Method addCoreAnimatedMorph:Int(pCoreAnimatedMorph:CalCoreAnimatedMorph)
		Local num:Int = m_vectorCoreAnimatedMorph.length
		
		For Local i:Int = 0 To num-1
		
			If m_vectorCoreAnimatedMorph[ i ] = Null Then
		
				m_vectorCoreAnimatedMorph[ i ] = pCoreAnimatedMorph
		
				Return i
		
			EndIf
		
		Next
		
		m_vectorCoreAnimatedMorph=m_vectorCoreAnimatedMorph[..num+1]
		m_vectorCoreAnimatedMorph[num]=pCoreAnimatedMorph
		
		Return num
	
	End Method
	
	
	Method removeCoreAnimatedMorph:Byte( id:Int )
	
		Local num:Int = m_vectorCoreAnimatedMorph.length
		If id >= num Or id < 0 Then Return False
		If m_vectorCoreAnimatedMorph[ id ] = Null Then Return False
		m_vectorCoreAnimatedMorph[ id ] = Null
		Return True
	
	End Method
	
	Method getCoreMorphAnimation:CalCoreMorphAnimation(coreMorphAnimationId:Int)
	
		If coreMorphAnimationId < 0 Or coreMorphAnimationId >= m_vectorCoreMorphAnimation.length
			DebugLog "Invalid Morph ID"
			Return Null
		EndIf
	
		Return m_vectorCoreMorphAnimation[coreMorphAnimationId]
		
	End Method

	Method getCoreAnimatedMorph:CalCoreAnimatedMorph(coreAnimatedMorphId:Int)
		If coreAnimatedMorphId < 0 Or coreAnimatedMorphId >= m_vectorCoreAnimatedMorph.length Or m_vectorCoreAnimatedMorph[ coreAnimatedMorphId ]  = Null Then
			Return Null
		EndIf
		
		Return m_vectorCoreAnimatedMorph[coreAnimatedMorphId]		
	End Method
	
	Method getCoreMorphAnimationCount:Int()
		Return m_vectorCoreAnimatedMorph.Length
	End Method
	
	Method addCoreMaterial:Int(coreMaterial:CalCoreMaterial)
	
		Local materialId = m_vectorCoreMaterial.length
		m_vectorCoreMaterial = m_vectorCoreMaterial[..materialId+1]'push(coreMaterial)
		m_vectorCoreMaterial[materialId] = coreMaterial
		Return materialId		
	
	End Method
	
	Method createCoreMaterialThread:Byte(coreMaterialThreadId:Int)
		'Make sure this is how this is used...
		m_mapmapCoreMaterialThread.Set(coreMaterialThreadId,New IntMap)
		Return True
	End Method
	
	Method getCoreMaterial:CalCoreMaterial(coreMaterialId:Int)
		If coreMaterialId < 0 Or coreMaterialId >= m_vectorCoreMaterial.length Then
			DebugLog "Invalid Material ID"
			Return Null
		EndIf
	
		Return m_vectorCoreMaterial[coreMaterialId]	
	End Method
	
	Method getCoreMaterialCount:Int()
		Return m_vectorCoreMaterial.length
	End Method

	Method getCoreMaterialId:Int(materialName:String)
	
		If m_materialName.Contains(materialName) = True Then
			Return Int(m_materialName.Get(materialName))
		Else
			Return -1
		EndIf
	
	End Method

	Method getCoreMaterialIdbyID:Int(materialId:Int,coreMaterialSetId:Int)
	
		If m_mapmapCoreMaterialThread.Contains(materialId) = True Then
			Local newmap:IntMap = IntMap(m_mapmapCoreMaterialThread.Get(materialId))
			If newmap.Contains(coreMaterialSetId) = True Then
				Return Int(newmap.Get(coreMaterialSetId).ToString())
			Else
				Return -1
			EndIf
		Else
			Return -1
		EndIf
		
	End Method

	Method loadCoreMaterial:Int(filename:String)
		DebugLog "Not implmented use Loader Type!"
		Return -1
	End Method
	
	Method unloadCoreMaterialbyID:Int(coreMaterialId:Int)
	
		If coreMaterialId < 0 Or coreMaterialId >= m_vectorCoreMaterial.length
			DebugLog "Invalid Material ID"
			Return -1
		EndIf
	
		m_vectorCoreMaterial[coreMaterialId] = Null
		Return coreMaterialId	
	
	End Method
	

	Method unloadCoreMaterial:Int(coreMaterialName:String)
	
		Local coreMaterialId:Int = getCoreMaterialId(coreMaterialName)
	
		If coreMaterialId < 0 Or coreMaterialId >= m_vectorCoreMaterial.length
			DebugLog "Invalid Material ID"
			Return -1
		EndIf
	
		m_vectorCoreMaterial[coreMaterialId] = Null
		Return coreMaterialId	
	
	End	Method
	
	Method saveCoreMaterial:Int(filename:String, coreMaterialId:Int)
		DebugLog "Not Implemented, Use Saver Type!"
		Return -1
	End Method
	
	Method setCoreMaterialId:Byte(coreMaterialThreadId:Int, coreMaterialSetId:Int, coreMaterialId:Int)
	
		If m_mapmapCoreMaterialThread.Contains(coreMaterialThreadId) = True Then
			Local coreMaterialThread:IntMap =IntMap( m_mapmapCoreMaterialThread.Get(coreMaterialThreadId))
			Return coreMaterialThread.Set(coreMaterialSetId,coreMaterialId)
		Else
			Return False
		EndIf
	
	End Method
	
	Method addMaterialName:Byte(name:String, coreMaterialId:Int)
	
	
		If coreMaterialId < 0 Or coreMaterialId >= m_vectorCoreMaterial.length Then
			DebugLog "Invalid Material ID"
			Return False
		EndIf
	
		m_vectorCoreMaterial[coreMaterialId].setName(name)
		m_materialName.Set(name,coreMaterialId)
		Return True		
	
	End Method
	
	
	Method addCoreMesh:Int(coreMesh:CalCoreMesh)
	
		Local meshId:Int = m_vectorCoreMesh.length
		m_vectorCoreMesh = m_vectorCoreMesh[..meshId+1]'push(coreMesh)
		m_vectorCoreMesh[meshId] = coreMesh
		Return meshId		
	
	End Method
	
	Method getCoreMesh:CalCoreMesh(coreMeshId:Int)
	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.length Then
			DebugLog "Invalid Mesh ID"
			Return Null
		EndIf
	
		Return m_vectorCoreMesh[coreMeshId]
	
	End Method
	
	Method getCoreMeshCount:Int()
		
		Return m_vectorCoreMesh.length
	End Method
	
	Method loadCoreMesh:Int(filename:String)
		DebugLog "Not Implemented, please use Loader Type"
		Return -1
	End Method
	
	Method unloadCoreMeshbyID:Int(coreMeshId:Int)

	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.length Then
			DebugLog "Invalid Mesh ID"
			Return -1
		EndIf
	
		m_vectorCoreMesh[coreMeshId] = Null
		Return coreMeshId
			
	End Method
	
	Method unloadCoreMesh:Int(coreMeshName:String)
		
		Local coreMeshId:Int = getCoreMeshId(coreMeshName)
	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.length Then
			DebugLog "Invalid Mesh ID"
			Return -1
		EndIf
	
		m_vectorCoreMesh[coreMeshId] = Null
		Return coreMeshId	
	End Method
	
		
	Method saveCoreMesh:Int(filename:String, coreMeshId:Int)
		DebugLog "Not Implemented, Use Saver Type"
		Return -1
	End Method


	Method addMeshName:Byte(meshName:String, coreMeshId:Int)
	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.length Then
			DebugLog "Invalid Mesh ID"
			Return False
		EndIf
	
		m_vectorCoreMesh[coreMeshId].setName(meshName)
		m_meshName.Set(meshName,coreMeshId)
		Return True		
	
	End Method
	
	Method getCoreMeshId:Int(meshName:String)
	
		If m_meshName.Contains(meshName) = True Then
			Return Int(m_meshName.Get(meshName))
		Else
			Return -1
		EndIf

	End Method
	
	Method makemodel:CalModel()'Don't really need this since native code was ported to monkey
	
		Return CalModel.FromCore(Self)

	End Method
	
	
	Method getCoreSkeleton:CalCoreSkeleton()
		Return m_coreSkeleton
	End Method
	
	Method loadCoreSkeleton:Int(filename:String)
		DebugLog "Not Implemented use Loader Type"
		Return -1
	End Method
	
	Method saveCoreSkeleton:Int(filename:String)
		DebugLog "Not Implemented use Saver Type"
	End Method
	
	Method setCoreSkeleton(coreSkeleton:CalCoreSkeleton)
		If coreSkeleton <> Null Then
			m_coreSkeleton = coreSkeleton
		Else
			DebugLog "Cannot set NULL Skeleton!"
		EndIf
	End Method
	
	Method addBoneName(boneName:String, boneId:Int)
	
		If m_coreSkeleton <> Null Then
		
			m_coreSkeleton.mapCoreBoneName(boneId, boneName)
		
		EndIf
	
	End Method
	
	Method getBoneId:Int(boneName:String)
	
		If m_coreSkeleton <> Null Then
			m_coreSkeleton.getCoreBoneId(boneName)
		Else
			Return -1
		EndIf
	
	End Method

End Type
